package org.ftc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.CodeUnit;
import org.ftc.form.CodeUnitForm;
import org.ftc.service.CodeUnitService;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class CodeUnitController {
	@Autowired
	UsersService usersService;
	@Autowired
	CodeUnitService codeUnitService;
	@Autowired
	FtcLogService logService;
	
	@RequestMapping(value = "/codeUnit.htm")
	public String codeUnit(ModelMap model) {
		return "codeUnit/list";
	}

	@RequestMapping(value = "/codeUnit/list.json", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> find(CodeUnitForm searchForm, BindingResult binding, ModelMap model) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		List<CodeUnit> data = codeUnitService.findAll();
		json.put("rows", data);
		logService.save(Log.QUERY, this.getClass().toString());
		return json;
	}

	@RequestMapping(value = "/codeUnit/update.json", method = RequestMethod.POST)
	public @ResponseBody String save(CodeUnit entity, ModelMap model) {
		logService.before();
		boolean result = false;
		if(entity==null || StringUtils.isBlank(entity.getName())){
			return "true";
		}else{
			CodeUnit codeUnit = codeUnitService.get(entity.getCode());
			if(codeUnit==null){
				result = codeUnitService.save(entity);
				logService.save(Log.INSERT, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
				return result?"save":"false";
			}else{
				codeUnit.setName(entity.getName());
				codeUnit.setSort(entity.getSort());
				result = codeUnitService.update(codeUnit);
				logService.save(Log.UPDATE, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
				return result?"update":"false";
			}
		}
	}
	
	@RequestMapping(value = "/codeUnit/delete/{codeUnitSeq}.json")
	public @ResponseBody String delete(@PathVariable String codeUnitSeq) {
		logService.before();
		Boolean s= false;
		if(StringUtils.isBlank(codeUnitSeq)){
			return s.toString();
		}
		s = codeUnitService.delete(codeUnitSeq);
		logService.save(Log.DELETE, codeUnitSeq, this.getClass().toString());
		return s.toString();  
	}
	
	@RequestMapping(value = "/codeUnit/findAll.json", method = RequestMethod.GET)
	public @ResponseBody
	List<CodeUnit> findAll() {
		return codeUnitService.findAll();
	}


}
